package com.ttctracker.map;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.ttctracker.classes.Direction;
import com.ttctracker.classes.PathPoint;
import com.ttctracker.classes.RoutePath;
import com.ttctracker.classes.Route;
import com.ttctracker.classes.StopItem;
public class PathHandler extends DefaultHandler{
	
	ArrayList<RoutePath> routePaths;
	RoutePath _currentPath;
	Route route;
	Direction _currentStopList;
	StopItem _currentStopItem;
	
	public PathHandler() {}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (localName.equals("path")) {
			routePaths.add(_currentPath);
			_currentPath=new RoutePath();
			
			return;
		}
		/*else if (localName.equals("stop")) {
			_currentStopList.add(_currentStopItem);
			//_currentStopItem=new StopItem();
			return;
		}
		else if (localName.equals("route")) {
			route.get_stops().add(_currentStopList);
			return;
		}*/
		else {
			return;
		}
	}

	

	public Route getRoute() {
		return route;
	}

	public ArrayList<RoutePath> getRoutePaths() {
		return routePaths;
	}

	@Override
	public void startDocument() throws SAXException {
		routePaths=new ArrayList<RoutePath>();
		route=new Route();
		//_currentPath=new RoutePath();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (localName.equals("path")) {
			_currentPath=new RoutePath();
			return;
		}else if (localName.equals("point")) {
			_currentPath.addPoint(new PathPoint(Double.parseDouble(attributes.getValue("lon")), Double.parseDouble(attributes.getValue("lat"))));
			return;
		}else if (localName.equals("route")) {
			route.set_tag(attributes.getValue("tag"));
			route.set_title(attributes.getValue("title"));
			route.set_minLat(Double.parseDouble(attributes.getValue("latMin")));
			route.set_minLon(Double.parseDouble(attributes.getValue("lonMin")));
			route.set_maxLat(Double.parseDouble(attributes.getValue("latMax")));
			route.set_maxLon(Double.parseDouble(attributes.getValue("lonMax")));
			_currentStopList=new Direction();
			return;
		}else if (localName.equals("stop")) {
			if(attributes.getValue("title")!=null){
				_currentStopItem=new StopItem();
				_currentStopItem.set_tag(attributes.getValue("tag"));
				//_currentStopItem.set_stopID(Integer.parseInt(attributes.getValue("stopId")));
				_currentStopItem.set_title(attributes.getValue("title"));
				_currentStopItem.set_lat(Double.parseDouble(attributes.getValue("lat")));
				_currentStopItem.set_lon(Double.parseDouble(attributes.getValue("lon")));
			}
			return;
		}
		else{
			return;
		}
		
	}


}
